package com.dennytech.tac.base;

import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.TextView;

import com.dennytech.tac.statistics.Stat;
import com.dennytech.tac.widget.SearchBarEditText;
import com.dennytech.tac.widget.SearchBarEditText.OnKeywordChangeListner;
import com.dennytech.tac.R;

public abstract class CFActivity extends FragmentActivity {

	protected boolean isDestoryed;

	protected TextView titleText;
	protected TextView subtitleText;
	protected ImageButton leftTitleButton;
	protected TextView leftTitleTextButton;
	protected ImageButton rightTitleButton;
	protected Button titleButton;
	protected SearchBarEditText searchEdt;

	@Override
	protected void onCreate(Bundle b) {
		super.onCreate(b);
		if (!Environment.isDebug()) {
			Stat.pageView(getClass().getName());
		}

		initCustomTitle();
		initView();
	}

	/**
	 * you must setContentView at first
	 */
	protected abstract void initView();

	@Override
	protected void onDestroy() {
		isDestoryed = true;
		super.onDestroy();
	}

	public static final int TITLE_TYPE_WIDE = 1;
	public static final int TITLE_TYPE_NONE = 2;
	public static final int TITLE_TYPE_ARROW = 3;
	public static final int TITLE_TYPE_EDIT_SEARCH = 4;
	public static final int TITLE_TYPE_BUTTON_SEARCH = 5;

	protected int customTitleType() {
		return TITLE_TYPE_WIDE;
	}

	protected void initCustomTitle() {
		switch (customTitleType()) {
		case TITLE_TYPE_WIDE:
			if (requestWindowFeature(Window.FEATURE_CUSTOM_TITLE)) {
				setContentView(R.layout.list_frame);
				getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,
						R.layout.wide_title_bar);
				leftTitleButton = (ImageButton) findViewById(R.id.left_title_button);
				leftTitleButton.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						finish();
					}
				});
				titleText = (TextView) findViewById(android.R.id.title);
				subtitleText = (TextView) findViewById(R.id.subtitle);
				rightTitleButton = (ImageButton) findViewById(R.id.right_title_button);
				titleButton = (Button) findViewById(R.id.title_button);
				searchEdt = (SearchBarEditText) findViewById(R.id.search_input);
				setTitle(getTitle());
			}
			break;

		case TITLE_TYPE_NONE:
			requestWindowFeature(Window.FEATURE_NO_TITLE);
			break;

		default:
			break;
		}

	}

	@Override
	public void setTitle(CharSequence title) {
		super.setTitle(title);
		if (titleText != null)
			titleText.setText(title);
	}

	public void setSubtitle(CharSequence title) {
		if (subtitleText != null)
			subtitleText.setText(title);
	}

	public void setLeftTitleButton(int resId,
			android.view.View.OnClickListener l) {
		if (leftTitleButton == null)
			return;
		if (resId == -1) {
			leftTitleButton.setVisibility(View.GONE);
		} else {
			leftTitleButton.setVisibility(View.VISIBLE);
			leftTitleButton.setImageResource(resId);
			leftTitleButton.setOnClickListener(l);
		}
	}

	public void setRightTitleButton(int resId,
			android.view.View.OnClickListener l) {
		if (rightTitleButton == null)
			return;
		if (resId == -1) {
			rightTitleButton.setVisibility(View.GONE);
		} else {
			rightTitleButton.setVisibility(View.VISIBLE);
			rightTitleButton.setImageResource(resId);
			rightTitleButton.setOnClickListener(l);
		}
	}

	public void setTitleButton(String text, android.view.View.OnClickListener l) {
		if (titleButton == null)
			return;
		if (text == null || "".equals(text))
			titleButton.setVisibility(View.GONE);
		else {
			titleButton.setVisibility(View.VISIBLE);
			titleButton.setText(text);
			titleButton.setOnClickListener(l);
		}
	}

	public void showSearchEdt(String hint, String keyword,
			boolean requestFocus, OnKeywordChangeListner l) {
		if (searchEdt != null) {
			searchEdt.setHint(hint);
			searchEdt.setVisibility(View.VISIBLE);
			searchEdt.setOnKeywordChangeListner(l);
			titleText.setVisibility(View.GONE);
			subtitleText.setVisibility(View.GONE);

			if (requestFocus) {
				searchEdt.requestFocus();
				InputMethodManager inputManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
				inputManager.toggleSoftInput(0,
						InputMethodManager.HIDE_NOT_ALWAYS);
			}

			searchEdt.setText(keyword);
			if (keyword != null) {
				searchEdt.setSelection(keyword.length());
			}
		}
	}

	public void showSearchEdt(int resId, String keyword, boolean requestFocus,
			OnKeywordChangeListner l) {
		showSearchEdt(getResources().getString(resId), keyword, requestFocus, l);
	}

	public void showSearchEdt(int resId, String keyword,
			OnKeywordChangeListner l) {
		showSearchEdt(getResources().getString(resId), keyword, true, l);
	}

	Dialog mDialog;

	public void showProgressDialog(String message,
			OnCancelListener cancleListener) {
		if (isDestoryed)
			return;
		dismissDialog();

		ProgressDialog dlg = new ProgressDialog(this);
		dlg.setOnCancelListener(cancleListener);
		dlg.setOnKeyListener(new DialogInterface.OnKeyListener() {
			@Override
			public boolean onKey(DialogInterface dialog, int keyCode,
					KeyEvent event) {
				if (keyCode == KeyEvent.KEYCODE_SEARCH)
					return true;
				return false;
			}
		});
		dlg.setMessage(message);

		mDialog = dlg;
		dlg.show();
	}

	public void dismissDialog() {
		if (isDestoryed)
			return;
		if (mDialog != null && mDialog.isShowing()) {
			mDialog.dismiss();
			mDialog = null;
		}
	}

	public void hideSearchEdt() {
		if (searchEdt != null) {
			searchEdt.setVisibility(View.GONE);
			titleText.setVisibility(View.VISIBLE);
			subtitleText.setVisibility(View.VISIBLE);
		}
	}

	@Override
	public void setContentView(int layoutResID) {
		super.setContentView(layoutResID);
		addTitleShadowView();
	}

	@Override
	public void setContentView(View view, LayoutParams params) {
		super.setContentView(view, params);
		addTitleShadowView();
	}

	@Override
	public void setContentView(View view) {
		super.setContentView(view);
		addTitleShadowView();
	}

	protected void addTitleShadowView() {
		FrameLayout rootFrame = (FrameLayout) findViewById(android.R.id.content);
		rootFrame.setBackgroundResource(R.drawable.main_background_light);
		View shadow = getLayoutInflater().inflate(R.layout.title_shadow,
				rootFrame, false);
		rootFrame.addView(shadow);
	}

}
